package ssh

import "sync"

type ConfigObserver struct {
	Changed   <-chan struct{}
	Unobserve func()
}

type Config struct {
	observers sync.Map
}

func (conf *Config) Observe() *ConfigObserver {
	changed := make(chan struct{}, 1)
	unobserve := func() {
		conf.observers.Delete(changed)
		close(changed)
	}
	conf.observers.Store(changed, nil)
	return &ConfigObserver{Changed: changed, Unobserve: unobserve}
}

func (conf *Config) Notify() {
	conf.observers.Range(func(key, value any) bool {
		select {
		case key.(chan struct{}) <- struct{}{}:
		default:
		}
		return true
	})
}
